<template>
	<view class="goodsInfo-container" v-if="goodsInfo.goods_name">
		<!-- 轮播图区域 -->
		<view class="goodsSwiper">
			<swiper :indicator-dots="true" :autoplay="true" :interval="2000" :duration="1000" :circular="true">
				<swiper-item v-for="(item,index) in goodsInfo.pics" :key="index">
					<image :src="item.pics_big" @click="preview(index)"></image>
				</swiper-item>
			</swiper>
		</view>
		<!-- 商品详情区域 -->
		<view class="goodsInfo">
			<!-- 商品价格 -->
			<view class="price">￥{{goodsInfo.goods_price}}</view>
			<!-- 信息主体区域 -->
			<view class="info">
				<!-- 商品名称 -->
				<view class="title">{{goodsInfo.goods_name}}</view>
				<!-- 收藏 -->
				<view class="collect">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="fare">快递：免运费</view>
		</view>
		<!-- 商品介绍图片区域 -->
		<view class="goodsIntroduction">
			<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		</view>
		<!-- 商品导航区域 -->
		<view class="goodsNav">
			<!-- fill 控制右侧按钮的样式 -->
			<!-- options 左侧按钮的配置项 -->
			<!-- buttonGroup 右侧按钮的配置项 -->
			<!-- click 左侧按钮的点击事件处理函数 -->
			<!-- buttonClick 右侧按钮的点击事件处理函数 -->
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
		</view>
		<!-- 加入购物车动画 -->
		<view class="animation" v-if="showAnimation">
			<image :src="firstImgUrl||defaultPic"></image>
		</view>
	</view>
</template>

<script>
	// 导入全局购物车数据
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		computed: {
			// 映射全局数据
			...mapState('my_cart', ['cart']),
			...mapGetters('my_cart', ['total'])
		},
		watch: {
			total(newTotal) {
				this.options[1].info = newTotal
			}
		},
		data() {
			return {
				// 默认图片地址
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				firstImgUrl: '',
				showAnimation: false,
				// 商品详情
				goodsInfo: {},
				// 左侧按钮组的配置对象
				options: [{
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "red"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				// 右侧按钮组的配置对象
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		onLoad(options) {
			this.options[1].info = this.total
			this.getGoodsInfo(options.goods_id)
		},

		methods: {
			// 映射全局方法
			...mapMutations('my_cart', ['addToCart']),
			// 获取商品详细数据
			async getGoodsInfo(goods_id) {
				const res = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				if (res.data.meta.status !== 200) return uni.$showMsg()

				// 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
				res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;" ').replace(
					/webp/g, 'jpg')
				// 如果没有图片 则展示默认图片
				if (res.data.message.pics.length === 0) {
					console.log('ok')
					res.data.message.pics = [{
						pics_big: this.defaultPic
					}]
				}
				this.goodsInfo = res.data.message
				this.firstImgUrl = this.goodsInfo.pics[0].pics_big
			},
			// 预览图片
			preview(index) {
				uni.previewImage({
					// 预览时，默认显示图片的索引
					current: index,
					// 所有图片 url 地址的数组
					urls: this.goodsInfo.pics.map(x => x.pics_big)
				})
			},
			// 底部导航左侧按钮点击事件
			onClick(e) {
				if (e.content.text === '购物车') {
					// 切换到购物车页面
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			// 底部导航右侧按钮点击事件
			buttonClick(e) {
				// 判断是否正在执行加入购物车动画
				if (!this.showAnimation) {
					const goodsInfo = {
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					// 执行加入购物车动画
					this.showAnimation = true
					setTimeout(() => {
						this.addToCart(goodsInfo)
						this.showAnimation = false
						if (e.content.text === '立即购买') {
							uni.switchTab({
								url: '/pages/cart/cart'
							})
						}
					}, 1000);
				}
			}
		}

	}
</script>

<style lang="scss">
	.goodsInfo-container {
		position: relative;
		padding-bottom: 100rpx;

		.goodsSwiper {
			swiper {
				height: 750rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.goodsInfo {
			padding: 20rpx;
			padding-right: 0;

			.price {
				color: #c00000;
				font-size: 36rpx;
				margin: 20rpx 0;
			}

			.info {
				display: flex;
				justify-content: space-between;

				.title {
					font-size: 26rpx;
					padding-right: 20rpx;
				}

				.collect {
					width: 240rpx;
					font-size: 24rpx;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					border-left: 2rpx solid #efefef;
					color: gray;
				}
			}

			.fare {
				margin: 20rpx 0;
				font-size: 24rpx;
				color: gray;
			}
		}

		.goodsIntroduction {}

		.goodsNav {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
		}

		.animation {
			position: fixed;
			width: 750rpx;
			height: 750rpx;
			top: 0;
			left: 0;

			image {
				width: 100%;
				height: 100%;
			}

			animation: addcart 1s linear;
		}

		@keyframes addcart {
			0% {
				transform: scale(1.0);
			}

			25% {
				transform: scale(0.5);
			}

			50% {
				transform: scale(0.1);
			}

			100% {
				transform: translate(-110px, 390px) scale(0.1);
			}
		}
	}
</style>
